package com.ruoyi.business.controller.ratelimit;

import com.ruoyi.business.annotations.ratelimit.GuavaRateLimit;
import com.ruoyi.business.annotations.ratelimit.RedisRateLimit;
import com.ruoyi.business.annotations.ratelimit.SentinelRateLimit;
import com.ruoyi.business.aspect.Redis.LimitType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: RateLimitTestController
 * @Author: Wu Yuwei
 * @Date: 2023/6/28
 */
@RestController
@RequestMapping("/ratelimit")
public class RateLimitTestController {

    /**
     * 测试Guava自定义注解限流
     * @return
     */
    @GetMapping("/guavaRateLimit")
    @GuavaRateLimit(limitType = "guavaRateLimit",limitCount = 1)
    public String guavaRateLimit(){
        try {
            Thread.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "success";
    }

    /**
     * 测试Sentinel自定义注解限流
     * @return
     */
    @GetMapping("/sentinelRateLimit")
    @SentinelRateLimit(resourceName = "sentinelRateLimit",limitCount = 1)
    public String sentinelRateLimit(){
        try {
            Thread.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "success";
    }

    /**
     * 测试Sentinel自定义注解限流
     * @return
     */
    @GetMapping("/redisRateLimit")
    @RedisRateLimit(key = "redisRateLimit",prefix = "ratelimit",count = 1,period = 1,limitType = LimitType.CUSTOMER)
    public String redisRateLimit(){
        try {
            Thread.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "success";
    }
}
